package com.example.houseitemmanagebackend.controller;

import com.example.houseitemmanagebackend.dto.CaptchaDTO;
import com.example.houseitemmanagebackend.dto.LoginDTO;
import com.example.houseitemmanagebackend.dto.PasswordDTO;
import com.example.houseitemmanagebackend.dto.RegisterDTO;
import com.example.houseitemmanagebackend.dto.UserProfileDTO;
import com.example.houseitemmanagebackend.dto.ResetPasswordDTO;
import com.example.houseitemmanagebackend.dto.EmailDTO;
import com.example.houseitemmanagebackend.dto.ChangePasswordDTO;
import com.example.houseitemmanagebackend.service.AuthService;
import com.example.houseitemmanagebackend.vo.Result;
import com.example.houseitemmanagebackend.vo.TokenVO;
import com.example.houseitemmanagebackend.vo.UserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Email;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 认证控制器
 */
@Tag(name = "认证管理", description = "用户认证相关接口")
@RestController
@RequestMapping("/auth")
@Validated
public class AuthController {

    @Autowired
    private AuthService authService;

    @Operation(summary = "用户登录", description = "用户登录接口，返回JWT令牌")
    @PostMapping("/login")
    public Result<TokenVO> login(@RequestBody @Validated LoginDTO loginDTO) {
        TokenVO tokenVO = authService.login(loginDTO);
        return Result.success(tokenVO);
    }

    @Operation(summary = "用户注册", description = "用户注册接口")
    @PostMapping("/register")
    public Result<Void> register(@RequestBody @Validated RegisterDTO registerDTO) {
        authService.register(registerDTO);
        return Result.success();
    }

    @Operation(summary = "获取当前用户信息", description = "获取当前登录用户的详细信息")
    @GetMapping("/user")
    public Result<UserVO> getCurrentUser(@RequestParam Long userId,Long locationId) {
        UserVO userVO = authService.getCurrentUser(userId,locationId);
        return Result.success(userVO);
    }

    @Operation(summary = "更新用户信息", description = "更新当前登录用户的个人资料")
    @PutMapping("/profile")
    public Result<Void> updateProfile(@Valid @RequestBody UserProfileDTO userProfileDTO) {
        authService.updateProfile(userProfileDTO);
        return Result.success();
    }

    @Operation(summary = "修改密码", description = "修改当前登录用户的密码")
    @PostMapping("/reset-password")
    public Result<?> resetPassword(@RequestBody @Validated ResetPasswordDTO resetPasswordDTO) {
        authService.resetPassword(resetPasswordDTO);
        return Result.success("密码重置成功");
    }

    @Operation(summary = "修改密码", description = "修改密码（需要原密码验证）")
    @PostMapping("/password")
    public Result<?> changePassword(@RequestBody @Validated ChangePasswordDTO changePasswordDTO) {
        authService.changePassword(changePasswordDTO);
        return Result.success("密码修改成功");
    }

    @Operation(summary = "发送验证码", description = "向指定邮箱发送验证码，用于重置密码")
    @PostMapping("/captcha")
    public Result<?> sendResetCode(@RequestBody @Validated EmailDTO emailDTO) {
        authService.sendResetCode(emailDTO.getEmail());
        return Result.success("重置密码验证码已发送，请检查您的邮箱或查看日志");
    }

}
